package com.zgscwjm.taobaphelper.ui;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.accessibility.AccessibilityEventCompat;
import android.util.Log;
import android.widget.Toast;

import com.zgscwjm.taobaphelper.UpdateActivity;
import com.zgscwjm.taobaphelper.b.a;
import com.zgscwjm.taobaphelper.exutils.MyBaseConfig;
import com.zgscwjm.taobaphelper.exutils.StringUtils;
import com.zgscwjm.taobaphelper.xp.FileManager;

/**
 * Created by fukunaka on 2017/3/10.
 */

public class handler_updateact extends Handler {


    private double c1 = 1048576.0d;
    private double c2 = 1024.0d;

    final UpdateActivity updateActivity;

    public handler_updateact(UpdateActivity updateActivity) {
        this.updateActivity = updateActivity;
    }

    @Override
    public void handleMessage(Message msg) {

        try {

            if (UpdateActivity.dialog != null) {
                UpdateActivity.dialog.dismiss();
            }
            String str;
            switch (msg.what) {
                case 0:
                    updateActivity.pg_down.setMax(updateActivity.r);
                    Log.i("文件长度---------->", String.valueOf(updateActivity.pg_down.getMax()));

                    return;
                case 1:
                    String str2;
                    this.updateActivity.pg_down.setProgress(this.updateActivity.s);
                    if (this.updateActivity.r > AccessibilityEventCompat.TYPE_TOUCH_INTERACTION_START) {
                        str2 = this.updateActivity.h.format((((double) this.updateActivity.r) / c1) + "MB");
                        str = this.updateActivity.h.format((((double) this.updateActivity.s) / c1) + "MB");
                    } else {
                        str2 = this.updateActivity.h.format((((double) this.updateActivity.r) / c2) + "KB");
                        str = this.updateActivity.h.format((((double) this.updateActivity.s) / c2) + "KB");

                    }
                    if (str.startsWith(".")) {
                        str = "0" + str;
                    }
                    this.updateActivity.tv_updateresulttip.setText("下载进度：" + str + "/" + str2);
                    return;
                case 500:
                    Toast.makeText(this.updateActivity.getApplicationContext(), "download taobao failed", Toast.LENGTH_SHORT).show();
                    this.updateActivity.tv_updateresulttip.setText("淘宝下载失败！");
                    return;
                case 501:
                    a.c("mv /data/data/" + MyBaseConfig.packageName + "／downloads/taobao.apk /data/local/tmp/taobao.apk");
                    a.chmod(FileManager.TBInstallPath, " 777 ");
                    if (UpdateActivity.Type == 506) {
                        this.updateActivity.tv_updateresulttip.setText("淘宝安装包下载成功！");
                        return;
                    }
                    this.updateActivity.tv_updateresulttip.setText("淘宝开始安装！");
                    str = a.c(" pm install -r /data/local/tmp/taobao.apk");
                    if (StringUtils.isEmpty(str)) {
                        this.updateActivity.tv_updateresulttip.setText("淘宝安装失败！");
                        return;
                    } else if (str.contains("uccess"))  // FIXME: 2017/3/13 怀疑这个应该是success
                    {
                        this.updateActivity.tv_updateresulttip.setText("淘宝安装成功！");
                        return;
                    } else {
                        this.updateActivity.tv_updateresulttip.setText("淘宝安装：" + str.replace("%OA", StringUtils.EMPTY) + "!");
                    }
                    return;
                case 502:
                    Toast.makeText(this.updateActivity.getApplicationContext(), "download config failed", Toast.LENGTH_SHORT).show();
                    this.updateActivity.tv_updateresulttip.setText(MyBaseConfig.Myappname + "更新失败！");
                    return;
                case 503:
                    a.c("mv /data/data/" + MyBaseConfig.packageName + "/downloads/config.apk /data/local/tmp/config.apk");
                    a.chmod(FileManager.ConfigInstallPath, "777");
                    Toast.makeText(this.updateActivity.getApplicationContext(), "install config:" + a.c(" pm install -r /data/local/tmp/config.apk"), Toast.LENGTH_SHORT).show();
                    return;
                case 504:
                    int a = UpdateActivity.getVersioncode(this.updateActivity, this.updateActivity.packgename);
                    this.updateActivity.i = ((Integer) msg.obj).intValue();
                    if (UpdateActivity.Type == 506 || this.updateActivity.o) {
                        this.updateActivity.tv_updateresulttip.setText("下载进度：0%");
                        new al(this.updateActivity, UpdateActivity.downloadurl, UpdateActivity.pathstring).start();
                        return;
                    } else if (this.updateActivity.i > a) {
                        UpdateActivity.getNewupdata(this.updateActivity);
                        return;
                    } else {
                        this.updateActivity.tv_updateresulttip.setText("当前已经是最新版本！");
                    }
                    return;
                case 509:
                    str = (String) msg.obj;
                    if (StringUtils.isEmpty(str)) {
                        this.updateActivity.tv_updateresulttip.setText("淘宝安装失败！");
                        return;
                    } else if (str.contains("success"))// FIXME: 2017/3/13 原文是uccess
                    {
                        this.updateActivity.tv_updateresulttip.setText("淘宝安装：" + str.replace("%0A", StringUtils.EMPTY) + "!");
                        return;
                    }
                    return;

                case 5021:
                    Toast.makeText(this.updateActivity.getApplicationContext(), "download sougou failed", Toast.LENGTH_SHORT).show();
                    this.updateActivity.tv_updateresulttip.setText("搜狗浏览器下载失败！");
                    return;
                case 5031:
                    com.zgscwjm.taobaphelper.b.a.c("mv /data/data/" + MyBaseConfig.packageName + "/downloads/sogou.apk /data/local/tmp/sogou.apk");
                    com.zgscwjm.taobaphelper.b.a.chmod(FileManager.SogouInstallPath, " 777 ");
                    if (UpdateActivity.Type == 506) {
                        this.updateActivity.tv_updateresulttip.setText("搜狗浏览器安装包下载成功！");
                    }
                    this.updateActivity.tv_updateresulttip.setText("搜狗浏览器开始安装！");
                    str = com.zgscwjm.taobaphelper.b.a.c(" pm install -r /data/local/tmp/sogou.apk");
                    if (StringUtils.isEmpty(str)) {
                        this.updateActivity.tv_updateresulttip.setText("搜狗浏览器安装失败！");
                        return;
                    } else if (str.contains("success")) {
                        this.updateActivity.tv_updateresulttip.setText("搜狗浏览器安装成功！");
                        return;
                    } else {
                        this.updateActivity.tv_updateresulttip.setText("搜狗浏览器安装：" + str.replace("%0A", StringUtils.EMPTY) + "!");
                    }
                    return;
                case 5041:
                    str = (String) msg.obj;
                    if (StringUtils.isEmpty(str)) {
                        this.updateActivity.tv_updateresulttip.setText("搜狗浏览器安装失败！");
                    } else if (str.contains("success")) {
                        this.updateActivity.tv_updateresulttip.setText("搜狗浏览器安装成功！");
                        return;
                    } else {
                        this.updateActivity.tv_updateresulttip.setText("搜狗浏览器安装：" + str.replace("%0A", StringUtils.EMPTY) + "!");
                    }
                    return;
                default:
                    return;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
